﻿using FlexFlow.Model;
using FlexFlow.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace FlexFlow.MVC.Controllers
{
    /// <summary>
    /// 表单模板API控制器
    /// </summary>
    [Auth(AuthType.API)]
    public class FormTemplateAPIController : Controller
    {
        /// <summary>
        /// 安装表单
        /// </summary>
        /// <param name="fileId">
        /// 表单文件ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult Install(Guid fileId, string name)
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            if (uinfo == null)
            {
                return Json(new OperateResult()
                {
                    ResultCode = 403,
                    Message = "游客不允许安装表单！"
                });
            }
            return Json(FormTemplateManager.Default.InstallTemplate(uinfo.UserId, fileId, name));
        }
        /// <summary>
        /// 查询名称中包含指定字符的表单模板集合
        /// </summary>
        /// <param name="nameLike">
        /// 要查询的表单模板名称
        /// </param>
        /// <param name="skip">
        /// 返回数据的起始位置
        /// </param>
        /// <param name="isAll">
        /// 一个值，表示返回的结果是所有的
        /// </param>
        /// <param name="limit">
        /// 限制数量
        /// </param>
        /// <returns>
        /// 查询结果
        /// </returns>
        public ActionResult QueryByName(string nameLike, int skip = 0, int limit = 5, bool isAll = false)
        {
            OperateResult res = FormTemplateManager.Default.QueryByName(nameLike, skip, limit, isAll);
            if (res.ResultCode != 200)
            {
                return Json(res);
            }
            else
            {
                List<object> tempList = new List<object>();
                foreach (var item in (List<FormTemplate>)res.Tag)
                {
                    tempList.Add(new { name = item.Name, id = item.Identity });
                }
                return Json(tempList, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 通过指定ID取得表单模板实例
        /// </summary>
        /// <param name="id">
        /// 要去的的表单模板的ID
        /// </param>
        /// <returns>
        /// 取得的表单模板
        /// </returns>
        public ActionResult GetByID(Guid id)
        {
            return Json(FormTemplateManager.Default.GetByID(id));
        }
        /// <summary>
        /// 取得指定的文件安装后的表单模板
        /// </summary>
        /// <param name="fileId">
        /// 要取得的表单模板的文件ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetByFileId(Guid fileId)
        {
            return Json(FormTemplateManager.Default.GetByFileId(fileId));
        }
    }
}
